const $common = require('../common/common-req.js');
const requestHandler = require('../common/request-handler.js');

// 用户Map
let broadcastQue = new Map()
const socketApi = {
    // 添加连接用户到ws队列
    addUser: (socket) => {
        // 提示用户可以传递用户信息了
        socket.emit('getUserData')
        // 接受用户身份信息并保存到队列
        socket.on('sendUserData', (data) => {
            if (!data.token) {
                return
            }
            console.log('用户信息', data)
            !broadcastQue.get(data.token) && broadcastQue.set(socket, {
                key: data.token,
                phone: data.phone,
                socket: socket
            })
            // getUserNotice(data.phone, socket)
        })
        // socket.on('updateUserCommentNotice', (data) => {
        //     let t_socket = broadcastQue.get(socket)
        //     getUserNotice(t_socket.phone, t_socket.socket)
        // })
    }
}
// 登录后获取用户登录信息
function getUserNotice(phone, socket) {
    let sql = 'select count(*) as total from commentreplyview where bePhone=? and isRead=0';
    let arr = [phone];
    $common.db_mysql.select(sql, arr, result => {
        console.log('获取结果成功', result)
        if (result) {
            $common.resData.data = {flag: result.length > 0};
            $common.resData.msg = '获取信息通知成功';
            // res.send($common.resData);
            socket.emit('getUserCommentNotice', result[0])
        } else {
            // res.send($common.setErrorData('获取信息通知失败'));
        }
    })
}
module.exports = socketApi